<style lang="less">
.hmCouponUseRecord {
    .ivu-modal {
        width: 660px !important;
    }
}
</style>
<template>
    <Modal v-model="show" :title="editTitle" class="hmCouponUseRecord" :mask-closable="false">
        <Form ref="hmCouponUseRecordForm" :model="hmCouponUseRecordForm" :rules="hmCouponUseRecordFormRule" :label-width="100">
            <FormItem label="用户优惠券id" prop="couponUserId">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUseRecordForm.couponUserId" placeholder="请输入用户优惠券id"/>
            </FormItem>
            <FormItem label="用户id" prop="userId">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUseRecordForm.userId" placeholder="请输入用户id"/>
            </FormItem>
            <FormItem label="订单流水号" prop="orderNumber">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUseRecordForm.orderNumber" placeholder="请输入订单流水号"/>
            </FormItem>
            <FormItem label="金额" prop="amount">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUseRecordForm.amount" placeholder="请输入金额"/>
            </FormItem>
            <FormItem label="使用时间" prop="useTime">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUseRecordForm.useTime" placeholder="请输入使用时间"/>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="text" @click="show=false">取消</Button>
            <Button type="primary" :loading="loading" @click="handSubmit" v-if="!disabled">提交</Button>
        </div>
    </Modal>
</template>
<script>
import {addHmCouponUseRecord, updateHmCouponUseRecord, getHmCouponUseRecord} from '@/api/hmzd/hmCouponUseRecord'

export default {
    name: "updateHmCouponUseRecord",
    props: {
        value: {
            type: Boolean,
            default: false
        },
        HmCouponUseRecordId: {
            type: String
        },
        modalTitle: {
            type: String
        }
    },
    data() {
        return {
            show: this.value,
            editTitle: this.modalTitle,
            loading: true,
            disabled: false,
            hmCouponUseRecordForm: {
                couponUserId: '',
                userId: '',
                orderNumber: '',
                amount: '',
                useTime: '',
            },
            hmCouponUseRecordFormRule: this.getHmCouponUseRecordFormRule()
        }
    },
    methods: {
        handSubmit() {
            this.$refs['hmCouponUseRecordForm'].validate((valid) => {
                this.loading = true;
                if (valid) {
                    if (this.HmCouponUseRecordId != null && this.HmCouponUseRecordId.trim().length > 0) {
                        this.hmCouponUseRecordForm.id = this.HmCouponUseRecordId;
                        updateHmCouponUseRecord(this.hmCouponUseRecordForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    } else {
                        addHmCouponUseRecord(this.hmCouponUseRecordForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    }
                } else {
                    this.loading = false;
                    this.$Message.error('表单验证不通过！');
                }
            });
        },
        closeModal(val) {
            this.$emit('input', val);
        },
        initForm() {
            this.hmCouponUseRecordForm = {
                couponUserId: '',
                userId: '',
                orderNumber: '',
                amount: '',
                useTime: '',
            };
        },
        getHmCouponUseRecordFormRule() {
            return {
                couponUserId: [
                    {required: true, message: '用户优惠券id不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                userId: [
                    {required: true, message: '用户id不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                orderNumber: [
                    {required: true, message: '订单流水号不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                amount: [
                    {required: true, message: '金额不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                useTime: [
                    {required: true, message: '使用时间不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
            }
        }
    },
    watch: {
        value(val) {
            this.show = val;
        },
        show(val) {
            this.initForm();
            this.loading = false;
            this.editTitle = this.modalTitle;
            if (this.editTitle == "查看") {
                this.disabled = true;
            } else {
                this.disabled = false;
            }
            if (val) {
                this.$refs['hmCouponUseRecordForm'].resetFields();
                this.$refs['hmCouponUseRecordForm'].id = null;
                if (this.HmCouponUseRecordId != null && this.HmCouponUseRecordId.trim().length > 0) {
                    getHmCouponUseRecord({id: this.HmCouponUseRecordId}).then(res => {
                        if (res.success) {
                            this.hmCouponUseRecordForm = res.data;
                        } else {
                            this.$Message.error(res.msg);
                        }
                    });
                }
            } else {
                this.closeModal(val);
            }
        }
    }
}
</script>
